﻿
(function($) {
	//给对象加验证
	var message = "请正确填写有关信息";

	$.fn.valTrim = function() {
		return $.trim($(this).val());
	}

	$.fn.verifyEmpty = function(pram) {
		if ($(this).valTrim().length == 0)
			throwError(this, pram);
	}

	$.fn.verifyLength = function( min, max ) {
		var n = $(this).valTrim().length;
		if (n < min)
			throwError(this, "最少填写" + min + "个字符");
		else if (n > max)
			throwError(this, "最多填写" + max + "个字符");
	}

	//整数
	$.fn.verifyInt = function(pram) {
		var reg = /^-?\d+$/;
		if (!reg.test($(this).valTrim()))
			throwError(this, pram);
	}

	$.fn.verifyFloat = function() {
		var reg = /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/;
		if (!reg.test($(this).valTrim()))
			throwError(this);
	}

	$.fn.verifyPassword = function( min, max, $id2) {
		$(this).verifyUsername(min, max);

		if ($(this).valTrim() != $id2.valTrim())
			throwError(this, "两次密码不一至");
	}

	$.fn.verifyDate = function() {
		$(this).verifyEmpty();

		//DD/MM/YYYY
		//var reg = /^(((0[1-9]|[12][0-9]|3[01])\/((0[13578]|1[02]))|((0[1-9]|[12][0-9]|30)\/(0[469]|11))|(0[1-9]|[1][0-9]|2[0-8])\/(02))\/([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}))|(29\/02\/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))$/;
		//YYYY-MM-DD
		var reg = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/;
		if (!reg.test($(this).valTrim()))
			throwError(this, "日期格式不正确 (YYYY-MM-DD)");
	}

	$.fn.verifyEmail = function() {
		$(this).verifyEmpty();

		var stremail = $(this).valTrim()
		if (stremail.length > 100)
			throwError(this, "邮箱太长");

		var reg = /^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*[0-9a-zA-Z]+))@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2}|net|NET|com|COM|gov|GOV|mil|MIL|org|ORG|edu|EDU|int|INT)$/;
		if (!reg.test(stremail))
			throwError(this, "邮箱格式不正确");
	}

	$.fn.verifyUsername = function(min, max) {
		$(this).verifyLength(min, max);

		var str = $(this).valTrim();
		var reg = /^[\w|\u4e00-\u9fa5]+$/;
		if (!reg.test(str))
			throwError(this, "用户名和密码只能是汉字，字母，数字和下划线");
	}

	$.fn.verifyChecked = function() {
		if (!$(this).attr("checked"))
			throwError(this);
	}

	function throwError(obj, msg) {
		obj.focus();
		throw (new Error(msg ? msg : message));
	}

})(jQuery);


// 页码控件， 输入参数data，JSON数据：
// ctrlid jquery对象字符串，如"#pager"
// page 整数，当前页，从0开始
// pages 页数
// pagenu 可选，显示页码的数量，最小5，默认显示15个页码。
function HPages(data) {
	//if (!data['ctrlid'] || !data['page'] || !data['pages']) {	// data['page'] 为0时直接返回
	//	return;
	//}

	var total = data['pages'];
	if (total <= 0)
		return;

	var current = data['page'];
	//	不验证，由上级程序保证
	//if (current < 0)
	//	current = 1;
	//else if (current > total)
	//	current = total;

	//current--; //以下从0开始
	var $o = $(data['ctrlid']);
	var num = data['pagenu'] ? data['pagenu'] : 11;

	var htmstr = "";
	if (total <= num) {	// 页数很少，全部显示
		for (var i = 0; i < total; ) {
			if (i == current)
				htmstr += "<a class='current'>" + ++i + "</a> ";
			else
				htmstr += "<a>" + ++i + "</a> ";
		}
	}
	else if (current < num / 2) {	// 当前页在前部，前面不显示翻页
		for (i = 0; i < num - 1; ) {
			if (i == current)
				htmstr += "<a class='current'>" + ++i + "</a> ";
			else
				htmstr += "<a>" + ++i + "</a> ";
		}
		htmstr += "<a>&gt;&gt;</a> <a>" + total + "</a>";
	}
	else if (total - current - 1 < num / 2) {	// 当前页在尾部部，后面不显示翻页
		htmstr += "<a>" + 1 + "</a> <a>&lt;&lt;</a> ";
		for (i = total - num + 1; i < total; ) {
			if (i == current)
				htmstr += "<a class='current'>" + ++i + "</a> ";
			else
				htmstr += "<a>" + ++i + "</a> ";
		}
	}
	else {	// 前后显示翻页
		htmstr += "<a>" + 1 + "</a> <a>&lt;&lt;</a> ";
		for (i = current - parseInt(num / 2) + 1, j = 0; j < num - 2; j++) {
			if (i == current)
				htmstr += "<a class='current'>" + ++i + "</a> ";
			else
				htmstr += "<a>" + ++i + "</a> ";
		}
		htmstr += "<a>&gt;&gt;</a> <a>" + total + "</a>";
	}

	htmstr += " &nbsp;第<input id='pageedit' type='text'/>页 <a id='pagego'>GO</a>"

	$o.html(htmstr);

	$o.children("a").click(function () {
		var pa = $(this).text();
		if (pa === 'GO') {
			pa = $o.children("#pageedit").val() - 1;
			if (!pa) {
				alert("请输入页码");
				return;
			}
		}
		else if (pa === '<<') {
			pa = current - num + 2;
		}
		else if (pa === '>>') {
			pa = current + num - 2;
		}
		else
			pa--;

		if (pa < 0)
			pa = 0;
		else if (pa >= total)
			pa = total - 1;

		if (data['gopage'])
			data['gopage'](pa);
	});
}

// 取地址栏参数
function getURLparameter(name) {
	var reg = new RegExp("(^|\\?|&)" + name + "=([^&|#]*)(\\s|&|$|#)", "i");
	if (reg.test(window.location))
		return unescape(RegExp.$2);
	return "";
}
